package com.shujia;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapred.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

// 读取本地score.txt文件 计算每个学生的总分 并将结果写入HBase
public class Demo6MRWriteHBase {
    public static class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            String[] splits = value.toString().split(",");
            String rowkey = splits[0];
            int score = Integer.parseInt(splits[2]);
            context.write(new Text(rowkey), new IntWritable(score));
        }
    }

    public static class WriteHBase extends TableReducer<Text, IntWritable, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, NullWritable, Mutation>.Context context) throws IOException, InterruptedException {
            int sum = 0; // 保存总分
            for (IntWritable value : values) {
                sum += value.get();
            }
            Put put = new Put(key.getBytes());
            put.addColumn("cf1".getBytes(), "ss".getBytes(), (sum + "").getBytes());
            context.write(NullWritable.get(), put);
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");
        Job job = Job.getInstance(conf);
        job.setJobName("Demo6MRWriteHBase");
        job.setJarByClass(Demo6MRWriteHBase.class);

        // 配置Map任务
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 使用TableMapReduceUtil配置Reduce任务
        TableMapReduceUtil.initTableReducerJob(
                "sum_score"
                , WriteHBase.class
                , job
        );

        // 配置输入路径
        FileInputFormat.addInputPath(job,new Path("/mrHBaseInput1"));

        job.waitForCompletion(true);
    }
    /**
     * 在HBase中建表：create 'sum_score','cf1'
     * 将score.txt 上传到 /mrHBaseInput1
     * 提交MR任务：
     * hadoop jar HBase-1.0-jar-with-dependencies.jar com.shujia.Demo6MRWriteHBase
     */
}
